package com.pristine.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pristine.dao.IOperatorDao;
import com.pristine.domain.OperatorEntity;
import com.pristine.service.wsdl.IOperatorService;
import com.pristine.service.wsdl.OperatorVO;
import com.pristine.util.EntityToVOConverter;
import com.pristine.util.VOToEntityConverter;

@Service("operatorService")
@Transactional
public class OperatorServiceImpl implements IOperatorService{

	@Autowired
	private IOperatorDao operatorDao;
	
	@Override
	public Integer createOperator(OperatorVO operatorVO){
		OperatorEntity operator = VOToEntityConverter.convert(operatorVO);
		operatorDao.add(operator);
		return operator.getOperatorId();
	}
	
	@Override
	public Integer updateOperator(OperatorVO operatorVO){
		OperatorEntity operator = VOToEntityConverter.convert(operatorVO);
		operatorDao.update(operator);
		
		return operator.getOperatorId();
	}
	
	@Override
	public boolean deleteOperator(Integer id){
		try{
			OperatorEntity entity = operatorDao.find(id);
			operatorDao.remove(entity);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	@Override
	public OperatorVO getOperatorById(Integer id){
		return EntityToVOConverter.convert(operatorDao.find(id));
	}
	
	@Override
	public List<OperatorVO> getAllOperator() {
		
		List<OperatorEntity> list = operatorDao.list();
		List<OperatorVO> voList= new ArrayList<OperatorVO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(OperatorEntity entity:list){
				voList.add(EntityToVOConverter.convert(entity));
			}
		}
		return voList;
	}
	
}
